package com.ymatou.validatecode.api.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.validatecode.api.parameter.ReSendMobileUserRegisterValidateCodeBean;
import com.ymatou.validatecode.api.service.ReSendMobileUserRegisterValidateCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 重新发送注册验证码 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ReSendMobileUserRegisterValidateCode {
	private static ReSendMobileUserRegisterValidateCodeBean vcb;
	private static ReSendMobileUserRegisterValidateCodeCall vcc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("重新发送注册验证码");
	}

	@Before
	public void caseUp() {
		vcb = new ReSendMobileUserRegisterValidateCodeBean();
		vcc = new ReSendMobileUserRegisterValidateCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(vcc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ReSendMobileUserRegisterValidateCode_001() {
		Logger.start(true,"重新发送注册验证码成功,不发送短信");
		try {
			String Phone = TestDataManager.getCommPhoneNum();
			vcb.setIsNeedSendValidateCodeToPhone(false);
			vcb.setPhone(Phone);
			vcc.setData(vcb);
			vcc.callService();
			Logger.comment("vcc send:" + vcc.getEntityString());
			Logger.comment("vcc response:" + vcc.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, vcc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.VERIFY_NOSEND, vcc.getMsg(), "验证msg");
			Logger.verifyEquals(true, vcc.isCreateValidateCodeSuccess(), "验证msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ReSendMobileUserRegisterValidateCode_002() {
		Logger.start(true,"重新发送注册验证码,发送短信");
		try {
			String Phone = TestDataManager.getCommPhoneNum();
			vcb.setIsNeedSendValidateCodeToPhone(true);
			vcb.setPhone(Phone);
			vcc.setData(vcb);
			vcc.callService();
			Logger.comment("vcc send:" + vcc.getEntityString());
			Logger.comment("vcc response:" + vcc.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, vcc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.VERIFY_SEND, vcc.getMsg(), "验证msg");
			Logger.verifyEquals(true, vcc.isCreateValidateCodeSuccess(), "验证msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ReSendMobileUserRegisterValidateCode_003() {
		Logger.start(true,"3分钟内发送频率超过100次，失败");
		try {
			String Phone = TestDataManager.getCommPhoneNum();
			vcb.setIsNeedSendValidateCodeToPhone(false);
			vcb.setPhone(Phone);
			for (int i = 0; i < 101; i++) {
				vcc = new ReSendMobileUserRegisterValidateCodeCall();
				vcc.setData(vcb);
				vcc.callService();
				Logger.debug(vcc.getReturnData());
			}
			Logger.comment("vcc send:" + vcc.getEntityString());
			Logger.comment("vcc response:" + vcc.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, vcc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.VERIFY_BLOCK, vcc.getMsg(), "验证msg");
			Logger.verifyEquals(false, vcc.isCreateValidateCodeSuccess(), "验证msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	@Ignore
	public void Tc_ReSendMobileUserRegisterValidateCode_004() {
		Logger.start(true,"3分钟内发送频率超过100次，失败后5分钟成功");
		try {
			String Phone = TestDataManager.getCommPhoneNum();
			vcb.setIsNeedSendValidateCodeToPhone(false);
			vcb.setPhone(Phone);
			for (int i = 0; i < 101; i++) {
				vcc = new ReSendMobileUserRegisterValidateCodeCall();
				vcc.setData(vcb);
				vcc.callService();
				Logger.debug(vcc.getReturnData());
			}
			Logger.comment("vcc send:" + vcc.getEntityString());
			Logger.comment("vcc response:" + vcc.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, vcc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.VERIFY_BLOCK, vcc.getMsg(), "验证msg");
			Logger.verifyEquals(false, vcc.isCreateValidateCodeSuccess(), "验证msg");
			// wait 5m
			Thread.sleep(5 * 60 * 1000 + 1000);
			vcc = new ReSendMobileUserRegisterValidateCodeCall();
			vcc.setData(vcb);
			vcc.callService();
			Logger.verifyEquals(ECode.SUCCESS, vcc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.VERIFY_NOSEND, vcc.getMsg(), "验证msg");
			Logger.verifyEquals(true, vcc.isCreateValidateCodeSuccess(), "验证msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}